package cn.iocoder.yudao.module.trade.api.brokerage;

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.trade.api.brokerage.dto.BrokerageUserDTO;
import cn.iocoder.yudao.module.trade.dal.dataobject.brokerage.BrokerageUserDO;
import cn.iocoder.yudao.module.trade.dal.mysql.brokerage.BrokerageUserMapper;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.BROKERAGE_BIND_OVERRIDE;

import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zyc
 * @createDate 2024/5/8 09:32
 * 推销用户 api接口实现类
 */
@Service
@Validated
public class TradeBrokerageUserApiImpl implements BrokerageUserApi{

    @Resource
    private BrokerageUserMapper brokerageUserMapper;

    /**
     * 获得推广员下的所有用户信息
     * @param id 推广员id
     * @return
     */
    @Override
    public List<Long> getUserIdList(Long id) {
        return brokerageUserMapper.getUserIdList(id);
    }

    /**
     * 获取推广员id
     * @param userId
     * @return
     */
    @Override
    public BrokerageUserDTO getBindUserId(Long userId) {
        BrokerageUserDO bindUserId = brokerageUserMapper.getBindUserId(userId);
        return BeanUtils.toBean(bindUserId,BrokerageUserDTO.class);
    }

    /**
     * 获取推广员id
     * @param userId
     * @return
     */
    @Override
    public BrokerageUserDTO getInfoByUserId(Long userId) {
        BrokerageUserDO brokerageUserDO = brokerageUserMapper.selectOne(new LambdaQueryWrapperX<BrokerageUserDO>()
                .eq(BrokerageUserDO::getId, userId));
        return BeanUtils.toBean(brokerageUserDO,BrokerageUserDTO.class);

    }

    /**
     * 创建上下级关系，注册时候创建
     * @param dto
     * @return
     */
    @Override
    public Integer addBrokerageUser(BrokerageUserDTO dto) {
        LambdaQueryWrapperX<BrokerageUserDO> wrapperX = new LambdaQueryWrapperX<>();
        wrapperX.eq(BrokerageUserDO::getId, dto.getId());
        wrapperX.eq(BrokerageUserDO::getBindUserId, dto.getBindUserTime());
        Long count = brokerageUserMapper.selectCount(wrapperX);
        if (count > 0) {
            throw exception(BROKERAGE_BIND_OVERRIDE);
        }

        LocalDateTime now = LocalDateTime.now();

        BrokerageUserDO brokerageUserDO = new BrokerageUserDO();
        brokerageUserDO.setId(dto.getId());
        brokerageUserDO.setBindUserId(dto.getBindUserId());
        brokerageUserDO.setBindUserTime(now);
        brokerageUserDO.setBrokerageTime(now);
        //默认没有分销资格
        Integer brokeragePrice = dto.getBrokeragePrice();
        if (brokeragePrice!=null && brokeragePrice > 0) {
            brokerageUserDO.setBrokeragePrice(brokeragePrice);
        }
        Integer frozenPrice = dto.getFrozenPrice();
        if (frozenPrice!=null && frozenPrice > 0) {
            brokerageUserDO.setFrozenPrice(frozenPrice);
        }
        brokerageUserDO.setBrokerageEnabled(true);

        brokerageUserDO.setBusinessType(dto.getBusinessType());
        brokerageUserDO.setUserBusiness(dto.getUserBusiness());
        brokerageUserDO.setBusinessStartTime(dto.getBusinessStartTime());
        brokerageUserDO.setBusinessEndTime(dto.getBusinessEndTime());

        brokerageUserDO.setTeamType(dto.getTeamType());
        brokerageUserDO.setUserTeam(dto.getUserTeam());
        brokerageUserDO.setTeamStartTime(dto.getTeamStartTime());
        brokerageUserDO.setTeamEndTime(dto.getTeamEndTime());

        return brokerageUserMapper.insert(brokerageUserDO);
    }

    /**
     * 修改佣金记录
     * @param data
     * @return
     */
    @Override
    public Boolean updateBrokerageUser(BrokerageUserDTO data) {
        BrokerageUserDO bean = BeanUtils.toBean(data, BrokerageUserDO.class);
        return brokerageUserMapper.updateById(bean) > 0;
    }

    /**
     * 获取推广信息
     * @param bindUserId
     * @param loginUserId
     * @return
     */
    @Override
    public BrokerageUserDTO getInfoByUserIds(Long bindUserId, Long loginUserId) {
        BrokerageUserDO data = brokerageUserMapper.getBindUserIds(bindUserId,loginUserId);
        return BeanUtils.toBean(data,BrokerageUserDTO.class);

    }
}
